package com.gitee.c0dehvb.reactive.sample.controller;

import com.gitee.c0dehvb.reactive.sample.common.DataResult;
import com.gitee.c0dehvb.reactive.sample.model.MediumCommentPO;
import com.gitee.c0dehvb.reactive.sample.repository.MediumCommentDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/{mediumSalesId}/comment")
public class MediumCommentController {
    @Autowired
    private MediumCommentDao mediumCommentDao;

    @GetMapping
    public Mono<DataResult<List<MediumCommentPO>>> list(@PathVariable("mediumSalesId") Long mediumSalesId) {
        return mediumCommentDao.findAllByMediumSalesId(mediumSalesId)
                .collectList()
                .map(e -> new DataResult<>("SUCCESS", "SUCCESS", e))
                .onErrorResume(e -> {
                    log.error("执行异常", e);
                    return Mono.just(new DataResult<>("ERROR", e.getMessage(), null));
                });
    }

    @GetMapping("/hint/{idx}")
    public Mono<DataResult<List<MediumCommentPO>>> list(@PathVariable("mediumSalesId") Long mediumSalesId,
                                                        @PathVariable("idx") Integer index) {
        return mediumCommentDao.findAllByMediumSalesId(mediumSalesId)
                .collectList()
                .map(e -> new DataResult<>("SUCCESS", "SUCCESS", e))
                .onErrorResume(e -> {
                    log.error("执行异常", e);
                    return Mono.just(new DataResult<>("ERROR", e.getMessage(), null));
                });
    }
}
